package com.example.bes.springboot.web.war.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@RestController
public class SystemPropertiesController {

    /**
     * 获取所有系统属性
     * @return
     */
    @GetMapping(value = "/sysprops")
    public Map<String, String> getAllProperties() {
        Properties properties = System.getProperties();
        Map<String, String> result = new HashMap<>();
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            result.put(entry.getKey().toString(), entry.getValue().toString());
        }
        return result;
    }

    /**
     * 获取指定系统属性
     * @param key
     * @return
     */
    @GetMapping(value = "/sysprops", params = "key")
    public Map<String, String> getPropertyByKey(@RequestParam String key) {
        String value = System.getProperty(key);
        Map<String, String> ressult = new HashMap<>();
        ressult.put(key, value != null ? value : "Not Found");
        return ressult;
    }
}
